package controller

import (
	"net/http"
	"server_cluster/common/logger"

	"github.com/gin-gonic/gin"
)

func (c *Controller) gmInfo(ctx *gin.Context) {
	token := ctx.GetHeader("token")
	if token == "" {
		ctx.JSON(http.StatusOK, GmTokenInvalid)
		return
	}
	gmInfo, err := c.s.GetGmInfo(token)
	if err != nil {
		logger.Error("get gm info error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	// 返回前端需要的格式
	ctx.JSON(200, gin.H{
		"code": 20000,
		"msg":  "",
		"data": gin.H{
			"roles":        []string{gmInfo.Role},                                                 // 转换为数组
			"name":         gmInfo.Name,                                                           // 小写字段名
			"avatar":       "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif", // 默认头像
			"introduction": "I am " + gmInfo.Name,                                                 // 生成简介
		},
	})
}
